import Vue from 'vue'

import 'normalize.css/normalize.css' // A modern alternative to CSS resets

import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import '@/styles/index.scss' // global css

import App from './App'
import router from './router'
import store from './store'

import i18n from './lang' // Internationalization
import '@/icons' // icon
import '@/permission' // permission control
import { removeToken } from './utils/auth'

Vue.use(ElementUI, {
  size: 'medium', // set element-ui default size
  i18n: (key, value) => i18n.t(key, value),
})

Vue.config.productionTip = false

const menu = window.sessionStorage.menu
if (menu) {
  const p = JSON.parse(menu)
  const routes = router.options.routes
  for (let i = 0; i < p.length; i++) {
    for (let j = 0; j < routes.length; j++) {
      if (routes[j].name ===  p[i].name) {
        if (router.options.routes[j].meta == null) {
          router.options.routes[j].meta = {}
        }
        router.options.routes[j].meta.permission = true
        router.options.routes[j].hidden = false
      } else if (routes[j].children) {
        const children = routes[j].children
        for (let k = 0; k < children.length; k++) {
          const children2 = children[k]
          if (children[k].name === p[i].name) {
            if (router.options.routes[j].children[k].meta == null) {
              router.options.routes[j].children[k].meta = {}
            }
            router.options.routes[j].children[k].meta.permission = true
            router.options.routes[j].children[k].hidden = false
          } else if (children2.children) {
            for (let k2 = 0; k2 < children2.children.length; k2++) {
              if (children2.children[k2].name === p[i].name) {
                if (router.options.routes[j].children[k].children[k2].meta == null) {
                  router.options.routes[j].children[k].children[k2].meta = {}
                }
                router.options.routes[j].children[k].children[k2].meta.permission = true
                router.options.routes[j].children[k].children[k2].hidden = false
              }
            }
          }
        }
      }
    }
  }
  console.log(router.options.routes)
} else {
  removeToken()
}
const tags = window.sessionStorage.tags
if (tags) {
  const p = JSON.parse(tags)
  for (let i = 0; i < p.length; i++) {
    store.dispatch('addVisitedViews', p[i])
  }
}

new Vue({
  el: '#app',
  router,
  store,
  i18n,
  render: h => h(App)
})
